package com.hzit.loan.pay.customer;

import com.hzit.loan.pay.commonApi.enums.NotifyResultEnum;
import com.hzit.loan.pay.commonApi.resp.PayNotifyData;
import com.hzit.loan.pay.config.MqConfig;
import com.hzit.loan.pay.config.PayConfig;
import com.hzit.loan.pay.model.NotifyResult;
import com.hzit.loan.pay.service.INotifyResultService;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

/**
 * 分布式事务：1.强一致性：tcc 2. seata, 3.可靠消息最终性
 *
 * 延时队列消费者.
 */
@Component
public class PayNotifyCustomer {


    private Logger logger = LoggerFactory.getLogger(PayNotifyCustomer.class);


    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private PayConfig payConfig;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private INotifyResultService notifyResultService;



    /**
     * 监听死信队列
     * @param channel
     * @param message
     * @param payNotifyData 队列取出的对象
     */
    @RabbitListener(queues = MqConfig.DEAD_PAY_NOTIFY_QUEUE_NAME)
    public void recive(Channel channel, Message message, PayNotifyData payNotifyData){
        logger.info("从队列获取到的对象：{}",payNotifyData);

        NotifyResult notifyResult = notifyResultService.queryByPaySerialNo(payNotifyData.getPaySerialNo());
        if(notifyResult.getCount() == 6){
            logger.info("{}，达到通知最大上限",payNotifyData.getPaySerialNo());
            return;
        }

        //再次通知
        String result = restTemplate.postForObject(payConfig.getNotifyUrl(),payNotifyData,String.class);

        if("success".equals(result)){

            try {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

                //更新success
                notifyResultService.updateNotifyResult(payNotifyData.getPaySerialNo(), NotifyResultEnum.NOTIFY_SUCCESS);
            } catch (IOException e) {
                logger.error("IOException",e);
            }
            return;
        }

        notifyResultService.updateNotifyResult(payNotifyData.getPaySerialNo(),NotifyResultEnum.NOTIFY_ING);

        long ttl = 0;

        if(notifyResult.getCount() == 1){
            ttl = 300000;
        }else if(notifyResult.getCount() == 2){
            ttl = 300000;

        } else if(notifyResult.getCount() == 3){
            ttl = 9000000;

        } else if(notifyResult.getCount() == 4){
            ttl = 2700000;
        } else if(notifyResult.getCount() == 5){
            ttl = 2700000;

        } else if(notifyResult.getCount() == 6){
            ttl = 2700000;
        }

        MessagePostProcessor myMessagePostProcessor = new MyMessagePostProcessor(ttl);

        //通知规则1+5ci ，第二次5，第 第一次5，三次 15分钟，第四次 45，第五次6个小时
        rabbitTemplate.convertAndSend(MqConfig.PAY_NOTIFY_EXCHANGE_NAME,"",payNotifyData,myMessagePostProcessor);

    }


}
